home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 1 Issue 2 / PDCD-1 - Issue 02.iso / _utilities / utilities / 003 / _gs / !GS / c / ZARITH < prev    next >
Text File  |  1991-10-25  |  6KB  |  260 lines

  1. /* Copyright (C) 1989, 1990, 1991 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* zarith.c */
  21. /* Arithmetic operators for GhostScript */
  22. #include "math_.h"
  23. #include "ghost.h"
  24. #include "errors.h"
  25. #include "oper.h"
  26. #include "store.h"
  27.  
  28. /****** NOTE: none of the arithmetic operators  ******/
  29. /****** currently check for floating exceptions ******/
  30.  
  31. /* Imported operators */
  32. extern int zcvi(P1(os_ptr));
  33.  
  34. /* Macro for accessing next-to-top stack element */
  35. #define opm1 (op-1)
  36. /* Macros for generating non-integer cases for arithmetic operations. */
  37. /* 'frob' is one of the arithmetic operators, +, -, or *. */
  38. #define non_int_cases(frob,frob_equals)\
  39.  switch ( r_type(op) ) {\
  40.   default: return e_typecheck;\
  41.   case t_real: switch ( r_type(opm1) ) {\
  42.    default: return e_typecheck;\
  43.    case t_real: opm1->value.realval frob_equals op->value.realval; break;\
  44.    case t_integer: make_real(opm1, opm1->value.intval frob op->value.realval);\
  45.   } break;\
  46.   case t_integer: switch ( r_type(opm1) ) {\
  47.    default: return e_typecheck;\
  48.    case t_real: opm1->value.realval frob_equals op->value.intval; break;\
  49.    case t_integer:
  50. #define end_cases()\
  51.   } }
  52.  
  53. /* add */
  54. int
  55. zadd(register os_ptr op)
  56. {    non_int_cases(+, +=)
  57.        {    long int2 = op->value.intval;
  58.         if ( ((opm1->value.intval += int2) ^ int2) < 0 &&
  59.              ((opm1->value.intval - int2) ^ int2) >= 0
  60.            )
  61.            {    /* Overflow, convert to real */
  62.             make_real(opm1, (float)opm1->value.intval - int2);
  63.            }
  64.        }
  65.     end_cases()
  66.     pop(1);
  67.     return 0;
  68. }
  69.  
  70. /* div */
  71. int
  72. zdiv(register os_ptr op)
  73. {    register os_ptr op1 = op - 1;
  74.     /* We can't use the non_int_cases macro, */
  75.     /* because we have to check explicitly for op == 0. */
  76.     switch ( r_type(op) )
  77.        {
  78.     default: return e_typecheck;
  79.     case t_real:
  80.         if ( op->value.realval == 0 ) return e_undefinedresult;
  81.         switch ( r_type(op1) )
  82.            {
  83.         default: return e_typecheck;
  84.         case t_real: op1->value.realval /= op->value.realval; break;
  85.         case t_integer: make_real(op1, op1->value.intval / op->value.realval);
  86.            }
  87.         break;
  88.     case t_integer:
  89.         if ( op->value.intval == 0 ) return e_undefinedresult;
  90.         switch ( r_type(op1) )
  91.            {
  92.         default: return e_typecheck;
  93.         case t_real: op1->value.realval /= op->value.intval; break;
  94.         case t_integer: make_real(op1, (float)op1->value.intval / op->value.intval);
  95.            }
  96.        }
  97.     pop(1);
  98.     return 0;
  99. }
  100.  
  101. /* mul */
  102. int
  103. zmul(register os_ptr op)
  104. {    non_int_cases(*, *=)
  105.        {    long int1 = opm1->value.intval;
  106.         long int2 = op->value.intval;
  107.         long abs1 = (int1 >= 0 ? int1 : - int1);
  108.         long abs2 = (int2 >= 0 ? int2 : - int2);
  109.         float fprod;
  110.         if (    (abs1 > 0x7fff || abs2 > 0x7fff) &&
  111.             /* At least one of the operands is very large. */
  112.             /* Check for integer overflow. */
  113.             abs1 != 0 &&
  114.             abs2 > 0x7fffffffL / abs1 &&
  115.             /* Check for the boundary case */
  116.             (fprod = (float)int1 * int2,
  117.              (int1 * int2 != -0x80000000L ||
  118.              fprod != (float)-0x80000000L))
  119.            )
  120.             make_real(opm1, fprod);
  121.         else
  122.             opm1->value.intval = int1 * int2;
  123.        }
  124.     end_cases()
  125.     pop(1);
  126.     return 0;
  127. }
  128.  
  129. /* sub */
  130. int
  131. zsub(register os_ptr op)
  132. {    non_int_cases(-, -=)
  133.        {    long int1 = opm1->value.intval;
  134.         if ( (int1 ^ (opm1->value.intval = int1 - op->value.intval)) < 0 &&
  135.              (int1 ^ op->value.intval) < 0
  136.            )
  137.            {    /* Overflow, convert to real */
  138.             make_real(opm1, (float)int1 - op->value.intval);
  139.            }
  140.        }
  141.     end_cases()
  142.     pop(1);
  143.     return 0;
  144. }
  145.  
  146. /* idiv */
  147. int
  148. zidiv(register os_ptr op)
  149. {    /* The Red Book says this only works on integers, */
  150.     /* but implementations also accept reals. */
  151.     ref save_num;
  152.     int code;
  153.     save_num = op[-1];
  154.     code = zdiv(op);
  155.     if ( code < 0 ) return code;    /* division failed */
  156.     code = zcvi(op - 1);
  157.     if ( code < 0 )
  158.        {    /* cvi failed, restore numerator */
  159.         op[-1] = save_num;
  160.         osp = op;        /* restore osp as well */
  161.        }
  162.     return code;
  163. }
  164.  
  165. /* mod */
  166. int
  167. zmod(register os_ptr op)
  168. {    check_type(op[-1], t_integer);
  169.     check_type(*op, t_integer);
  170.     if ( op->value.intval == 0 ) return e_undefinedresult;
  171.     op[-1].value.intval %= op->value.intval;
  172.     pop(1);
  173.     return 0;
  174. }
  175.  
  176. /* neg */
  177. int
  178. zneg(register os_ptr op)
  179. {    switch ( r_type(op) )
  180.        {
  181.     default: return e_typecheck;
  182.     case t_real: op->value.realval = -op->value.realval; break;
  183.     case t_integer:
  184.         if ( op->value.intval == -0x80000000L )    /* min integer */
  185.             make_real(op, -(float)-0x80000000L);
  186.         else
  187.             op->value.intval = -op->value.intval;
  188.        }
  189.     return 0;
  190. }
  191.  
  192. /* ceiling */
  193. int
  194. zceiling(register os_ptr op)
  195. {    switch ( r_type(op) )
  196.        {
  197.     default: return e_typecheck;
  198.     case t_real: op->value.realval = ceil(op->value.realval);
  199.     case t_integer: ;
  200.        }
  201.     return 0;
  202. }
  203.  
  204. /* floor */
  205. int
  206. zfloor(register os_ptr op)
  207. {    switch ( r_type(op) )
  208.        {
  209.     default: return e_typecheck;
  210.     case t_real: op->value.realval = floor(op->value.realval);
  211.     case t_integer: ;
  212.        }
  213.     return 0;
  214. }
  215.  
  216. /* round */
  217. int
  218. zround(register os_ptr op)
  219. {    switch ( r_type(op) )
  220.        {
  221.     default: return e_typecheck;
  222.     case t_real: op->value.realval = floor(op->value.realval + 0.5);
  223.     case t_integer: ;
  224.        }
  225.     return 0;
  226. }
  227.  
  228. /* truncate */
  229. int
  230. ztruncate(register os_ptr op)
  231. {    switch ( r_type(op) )
  232.        {
  233.     default: return e_typecheck;
  234.     case t_real:
  235.         op->value.realval =
  236.             (op->value.realval < 0.0 ?
  237.                 ceil(op->value.realval) :
  238.                 floor(op->value.realval));
  239.     case t_integer: ;
  240.        }
  241.     return 0;
  242. }
  243.  
  244. /* ------ Initialization table ------ */
  245.  
  246. op_def zarith_op_defs[] = {
  247.     {"2add", zadd},
  248.     {"1ceiling", zceiling},
  249.     {"2div", zdiv},
  250.     {"2idiv", zidiv},
  251.     {"1floor", zfloor},
  252.     {"2mod", zmod},
  253.     {"2mul", zmul},
  254.     {"1neg", zneg},
  255.     {"1round", zround},
  256.     {"2sub", zsub},
  257.     {"1truncate", ztruncate},
  258.     op_def_end(0)
  259. };
  260.